﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Text;

namespace Campus
{
    #region Type Definitions
    /// <summary>
    /// The different states a MenuButton can have relative to interaction
    /// with the mouse.
    /// </summary>
    public enum MenuButtonState { idle, rollover, active, invalid, invisible };

    /// <summary>
    /// This is the delegate used for MenuButtons.  When the MenuButton
    /// is clicked, it will invoke this delegate's method.
    /// </summary>
    /// <param name="option"></param>
    public delegate void ButtonDelegate(string option);

    #endregion

    class MenuButton
    {
        #region Fields and Properties

        //private ButtonDelegate m_buttonDelegate;
        private Dictionary<MenuButtonState, Texture2D> m_stateTextures;

        /// <summary>
        /// The texture (graphic) for the MenuButton object.
        /// </summary>
        public Texture2D Texture 
        {
            get
            {
                if (m_stateTextures.ContainsKey(State))
                {
                    return m_stateTextures[State];
                }
                else if (m_stateTextures.ContainsKey(MenuButtonState.idle))
                {
                    return m_stateTextures[MenuButtonState.idle];
                }
                else
                {
                    return null;
                }
            } 
        }

        /// <summary>
        /// The position given within the MouseMenu's coordinate system.
        /// </summary>
        public Vector2 Position { get; private set; }

        /// <summary>
        /// The name of the MenuButton.  Not only is it the name, but it
        /// passes this string into the argument when invoking the delegate.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// The current state the button is in.
        /// </summary>
        public MenuButtonState State { get; set; }

        /// <summary>
        /// The delegate to invoke when the button is pressed.
        /// </summary>
        public ButtonDelegate Delegate { private get; set; }

        /// <summary>
        /// A boolean determining whether a button is 'selected' or not.  'Selected' buttons
        /// will be drawn with the 'SelectedColor' property in its containing MouseMenu.
        /// </summary>
        public bool Selected { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a button to be used in conjunction with a MouseMenu object.
        /// </summary>
        /// <param name="buttonTextures">The Dictionary that pairs MenuButtonStates with Texture2D's you wish
        /// to be shown for the appropriate MenuButtonState.</param>
        /// <param name="position">The position, relative to the upper left corner of the owning MouseMenu object.</param>
        /// <param name="name">The name of the button.  This is also the value passed to the delegate when the 
        /// delegate is invoked.</param>
        /// <param name="buttonDelegate">The function to call when the button is pressed.</param>
        public MenuButton(Dictionary<MenuButtonState, Texture2D> buttonTextures, Vector2 position, string name, ButtonDelegate buttonDelegate)
        {
            this.Position = position;
            this.Name = name;
            this.State = MenuButtonState.idle;
            this.Delegate = buttonDelegate;

            this.m_stateTextures = buttonTextures;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Invokes the MenuButton's delegate.
        /// </summary>
        public void InvokeButton()
        {
            Delegate.Invoke(Name);
            Campus.ReloadActionMenu = true;
        }

        #endregion
    }
}
